//
// Mediator.ts
// Author:qiuchun(362460803@qq.com)
// Time:2023.04.30 23:02:39
// Desc:视图中间类，游戏组件通过该类发送消息

import Facade from "./Facade";
import Notification from "./Notification";
import Notifier from "./Notifier";

type func = (notification: Notification) => void;
type func1 = () => string[];

export default class Mediator extends Notifier {
    private _handle: func = null;
    private _listNotification: func1 = null;
    private _context: object = null;

    //设置一个监听函数，让组件可以接受到mvp的消息。
    public setHandleNotification(context: object, handle: func, listNotification: func1) {
        this._context = context;
        this._handle = handle;
        this._listNotification = listNotification;
    }

    //监听上应避免直接监听view的消息
    public handleNotification(noti: Notification): void {
        if (this._context && this._handle) {
            this._handle.call(this._context, noti);
        }
    }

    //消息队列，注册时会自动绑定
    public listNotificationInterests() {
        if (this._context && this._listNotification) {
            return this._listNotification.call(this._context);
        }
        else {
            let list: Array<string> = [];
            return list;
        }
    }

    //给Presenter广播消息
    //理论上，这个也可以给Meiator广播消息。但是，暂时想不到怎样的消息是可以直接发送给view，而不用不经过presenter处理？
    sendNotification(name: string, body?: object, type?: string, dest?: Notifier | string) {
        Facade.getInstance().sendNotification(name, body, type, dest);
    };
}

